package org.PISS.viewComponents.productProcessing;

import org.PISS.controller.model.Product;
import org.PISS.services.DAOService;
import org.primefaces.context.RequestContext;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import java.io.Serializable;
import java.util.List;

/**
 * Created by admin on 28.12.13.
 */

@ManagedBean
@ViewScoped
public class ProductTableBean implements Serializable {

    private List<Product> productList;
    private List<Product> filteredProductList;
    private List<Product> containsProductList;
    private Product selectedProduct;
    private boolean reselected=true;

    public List<Product> getProductList() {
        if(productList==null){
            productList= DAOService.getInstance().getProductDAO().getProducts();
        }
        return productList;
    }

    public List<Product> getContainsProductList() {
        if(reselected && selectedProduct!=null){
            containsProductList=DAOService.getInstance().getProductDAO().getContainsProducts(selectedProduct.getProductID());
            reselected=false;
        }
        return containsProductList;
    }

    public void setContainsProductList(List<Product> containsProductList) {
        this.containsProductList = containsProductList;
    }

    public Product getSelectedProduct() {
        return selectedProduct;
    }

    public void setSelectedProduct(Product selectedProduct) {
        this.selectedProduct = selectedProduct;
        reselected=true;
        RequestContext.getCurrentInstance().update("detail_product_form:detail_dialog");
        RequestContext.getCurrentInstance().execute("detail_product_form:detail_dialog.show()");
    }

    public void setProductList(List<Product> productList) {
        this.productList = productList;
    }

    public List<Product> getFilteredProductList() {
        return filteredProductList;
    }

    public void setFilteredProductList(List<Product> filteredProductList) {
        this.filteredProductList = filteredProductList;
    }

}
